<template>
	<div class="wrap">
		<!-- 搜索条件 -->
		<el-form ref="form" :inline="true" size="small" :model="forms" class="form" label-width="100px">
			<el-form-item label="标题" prop="ques_title" label-width="70px">
				<el-input
					v-model="forms.ques_title"
					placeholder="请输入标题"
					clearable
					style="width: 200px"
				/>
			</el-form-item>
			<el-form-item label="开始时间" prop="date">
				<el-date-picker
					v-model="forms.date"
					type="daterange"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期"
					value-format="yyyy-MM-dd"
				></el-date-picker>
			</el-form-item>
			<p class="line">
				<el-button icon="el-icon-search" type="primary" size="small" @click="loadData">
					搜索
				</el-button>
			</p>
		</el-form>
		<div class="answer-bot">
			<div style="padding-bottom: 10px">
				<el-button
					type="primary"
					size="small"
					plain
					@click="$router.push({ path: '/questions/create' })"
				>
					新增题目
				</el-button>
			</div>
			<el-table
				:data="tableData"
				style="margin-top: 5px"
				stripe
				border
				:cell-style="() => ({ textAlign: 'center' })"
				:header-cell-style="() => ({ textAlign: 'center' })"
			>
				<el-table-column label="题目编号" prop="ques_id" min-width="100px" show-overflow-tooltip />
				<el-table-column label="标题" prop="ques_title" min-width="160px" show-overflow-tooltip />
				<el-table-column
					label="题目类型"
					min-width="120px"
					show-overflow-tooltip
					:formatter="row => (row.ques_type === 1 ? '多选题' : '单选题')"
				/>
				<el-table-column
					label="创建时间"
					prop="add_time"
					min-width="180px"
					show-overflow-tooltip
					align="center"
				/>
				<el-table-column
					label="题目状态"
					prop="cid_no"
					min-width="100px"
					show-overflow-tooltip
					:formatter="row => (row.is_close === 1 ? '禁用' : '启用')"
				/>
				<el-table-column label="操作" width="210" align="center">
					<template slot-scope="scope">
						<el-link
							type="primary"
							:underline="false"
							@click="$router.push({ path: '/questions/create', query: { id: scope.row.ques_id } })"
						>
							编辑
						</el-link>
						<el-link type="primary" :underline="false" @click="onDelete(scope.row, scope.$index)">
							删除
						</el-link>
					</template>
				</el-table-column>
			</el-table>
			<el-row type="flex" justify="center">
				<el-col :span="24" style="text-align: right">
					<Pagination
						:total="total"
						:page.sync="pageNo"
						:limit.sync="pageSize"
						scroll-el="el-main"
						@pagination="pageChange"
					/>
				</el-col>
			</el-row>
		</div>
	</div>
</template>

<script>
	import { questionDelete, questionList } from '@/api/system.js'
	import Pagination from '@/components/Pagination'
	export default {
		components: { Pagination },
		data() {
			return {
				forms: {
					ques_title: '',
					date: []
				},
				pageNo: 1,
				pageSize: 10,
				total: 0,
				tableData: []
			}
		},
		created() {
			this.searchData()
		},
		methods: {
			// 搜索
			searchData() {
				this.pageNo = 1
				this.loadData()
			},
			// 分页
			pageChange({ page, limit }) {
				this.pageNo = page
				this.pageSize = limit
				this.loadData()
			},
			loadData() {
				const loading = this.$setLoading()
				this.tableData = []
				questionList({
					ques_title: this.forms.race_name,
					start_time: this.forms.date && this.forms.date.length ? this.forms.date[0] : '',
					end_time: this.forms.date && this.forms.date.length ? this.forms.date[1] : '',
					page_no: this.pageNo,
					page_size: this.pageSize
				}).then(
					res => {
						this.tableData = res.data.lists
						this.total = Number(res.data.total)
						loading.close()
					},
					err => {
						loading.close()
					}
				)
			},
			onDelete(row, index) {
				this.$confirm('删除后不可恢复，确定删除吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					dangerouslyUseHTMLString: true,
					type: 'warning'
				})
					.then(async () => {
						const loading = this.$setLoading({ text: '提交中~' })
						try {
							questionDelete({ ques_id: row.ques_id }).then(res => {
								this.$message.success(`删除成功！`)
								this.tableData.splice(index, 1)
							})
							loading.close()
						} catch (e) {
							console.log(e)
							loading.close()
						}
					})
					.catch(() => {})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.app-container {
		.roles-table {
			margin-top: 30px;
		}

		.tree-label {
			font-size: 14px;
			color: #606266;
			font-weight: bolder;
			padding-left: 9px;
		}

		.permission-tree {
			margin: 30px;
		}
	}

	.el-link {
		margin: 0 5px;
	}
	.form {
		background: #ffffff;
		padding: 20px 10px 0;
		margin-bottom: 10px;
		border-radius: 2px;
		display: flex;
		flex-wrap: wrap;
	}
	.line {
		height: 36px;
		margin: 0 0 10px 18px;
		border-left: 1px solid #e5e6eb !important;
		padding: 0 0 0 18px;
	}
	.answer-bot {
		padding: 16px;
		border-radius: 2px;
		background-color: #ffffff;
		.bot-title {
			font-size: 16px;
			font-family: PingFangSC-Regular, PingFang SC;
			color: #000000;
		}
	}
</style>
